package com.logistics.baiqun;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBar;

import com.shadow.baiqun.delegate.HomeDelegate;
import com.shadow.baiqun.delegate.LoginDelegate;
import com.shadow.baiqun.delegate.RootDelegate;
import com.shadow.baiqun.event.CheckVersionEvent;
import com.shadow.baiqun.event.ScanInfoEvent;
import com.shadow.baiqun.listener.ISignListener;
import com.shadow.core.activity.ProxyActivity;
import com.shadow.core.delegate.ShadowDelegate;
import com.tencent.bugly.beta.Beta;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import qiu.niorgai.StatusBarCompat;

/**
 * @author zy
 * Create on 2018/5/14 22:40
 */
public class MainActivity extends ProxyActivity implements ISignListener {
    private final static String SCAN_ACTION = "scan.rcv.message";

    @Override
    public ShadowDelegate setRootDelegate() {
        return RootDelegate.create();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.app_background));
        IntentFilter filter = new IntentFilter();
        filter.addAction(SCAN_ACTION);
        registerReceiver(mScanReceiver, filter);
    }

    @Override
    public void onSignInSuccess() {
        startWithPop(new HomeDelegate());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unregisterReceiver(mScanReceiver);
    }

    @Subscribe
    public void onCheckVersion(CheckVersionEvent event) {
        Beta.checkUpgrade(true, false);
    }

    @Override
    public void onSignInFailed() {
        startWithPop(new LoginDelegate());
    }

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
        @SuppressWarnings("SpellCheckingInspection")
        @Override
        public void onReceive(Context context, Intent intent) {
            byte[] barocode = intent.getByteArrayExtra("barocode");
            int barocodelen = intent.getIntExtra("length", 0);
            String barcodeStr = new String(barocode, 0, barocodelen);
            EventBus.getDefault().post(new ScanInfoEvent(barcodeStr));
        }
    };
}
